<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <style>
        label.required:after { /* 给红色的必填项打红色星号（文字后）如果打前面用before*/
            content: ' *';
            color: red;
        }

        .layui-form-select dl {
            max-height: 260px;
        }

        .layui-disabled, .layui-disabled:hover {
            color: black !important;
        }
    </style>
</head>
<body class="pear-container">
<form class="layui-form" action="">
    <div class="layui-card">
        <div class="layui-card-body" style="min-height: calc(100vh - 40px);">
            <blockquote class="layui-elem-quote">归还档案名&nbsp;&nbsp;<span style="color: red" th:text="${fileName}"></span>&nbsp;&nbsp;默认是原位置,可以修改
            </blockquote>
            <div class="layui-form-item">
                <label class="layui-form-label" style="display:none">id</label>
                <div class="layui-input-block" style="display:none">
                    <input type="text" th:value="${uuid}" name="uuid" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">档案名称</label>
                <div class="layui-input-block">
                    <input type="text" name="fileName" required lay-verify="required" autocomplete="off"
                           th:value="${fileName}"
                           placeholder="请输入档案名称" disabled readonly
                           class="layui-input layui-disabled" lay-reqtext="档案名称为必填项">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">
                    归还部门
                </label>
                <div class="layui-input-block">
                    <input type="number" id="deptId" name="deptId" lay-verify="required"
                           lay-reqtext="归还部门为必填项"
                           style="display:none;width: 0px" autocomplete="off" class="layui-input">
                    <ul id="dataTree" class="dtree" data-id="0"></ul>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">用途</label>
                <div class="layui-input-block">
                            <textarea name="purpose" placeholder="请输入备注" class="layui-textarea"
                                      style="min-height: 65px!important;"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">位置</label>
                <div class="layui-input-block">
                    <div id="position"></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="returnFileSave">保存</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </div>
</form>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script src="/xm-select.js"></script>
<script th:inline="javascript">
    var uuid = [[${uuid}]];
    var $ = layui.jquery;

    var position = xmSelect.render({
        el: '#position',
        // size: 'large',
        theme: {
            color: '#0081ff',
        },
        autoRow: true,
        filterable: true,
        tips: '请选择档案位置',
        style: {
            minHeight: '38px',
            height: '38px',
            fontSize: '16px'
        },
        tree: {
            show: true,
            showFolderIcon: true,
            showLine: true,
            indent: 20,
        },
        model: {
            label: {
                // type: 'block',
                block: {
                    //最大显示数量, 0:不限制
                    showCount: 6,
                    //是否显示删除图标
                    showIcon: true,
                }
            }
        },
        toolbar: {
            show: true,
            list: ['ALL', 'REVERSE', 'CLEAR']
        },
        height: '180px',
        showCount: 5,
        data: function () {
            return []
        }
    })
    $(document).ready(function () {
        // 当文档加载完毕后执行的代码
        $.ajax({
            url: '/file/manage/position/return/tree?uuid=' + uuid,
            type: 'get',
            success: function (result) {
                position.update({
                    data: result
                })

            }
        })
    });


    layui.use(['form', 'jquery', 'laydate', 'dtree'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let laydate = layui.laydate;
        let dtree = layui.dtree;

        laydate.render({
            elem: '#borrowingDate' //指定元素
            , theme: '#393D49',
        });

        // 初始化树
        dtree.render({
            elem: "#dataTree",
            initLevel: "1",//默认展开层级为1
            width: "100%",
            method: 'get',
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            // response:{message:"msg",statusCode:200},  //修改response中返回数据的定义
            url: "/dept/buildDeptAll",
            dataFormat: "list",  //配置data的风格为list
            select: true, //指定下拉树模式
            selectTips: "请选择部门",
            selectCardHeight: "150"
        });
        dtree.on("node('dataTree')", function (obj) {
            var param = dtree.getNowParam("dataTree");
            $("#deptId").val(param.nodeId);
        });
        //第一次默认渲染
        // columnSelect(layerMap[currentLayer])
        //
        // function columnSelect(columnList) {
        //     let insertHtml = '<option value="">请选择列</option>';
        //     for (let column in columnList) {
        //         if (columnList[column] == currentColumn) {
        //             insertHtml += '<option value="' + columnList[column] + '" selected>' + columnList[column] + '</option>';
        //         } else {
        //             insertHtml += '<option value="' + columnList[column] + '">' + columnList[column] + '</option>';
        //         }
        //
        //     }
        //     $("#columnSelect").html(insertHtml);
        //     form.render('select'); //刷新select选择框渲染
        // }

        //动态渲染下拉框
        // form.on('select(layerChange)', function (data) {
        //     let selectVal = data.value //得到被选中的值
        //     let columnList = layerMap[selectVal];
        //     columnSelect(columnList)
        // });
        form.on('submit(returnFileSave)', function (data) {
            let select = position.getValue("valueStr");
            console.log(select)
            if (select.length === 0) {
                position.warning('#f80808', true);
                layer.msg("请选择位置", {icon: 2, time: 1800});
            } else {
                let loading = layer.load();
                $.ajax({
                    url: '/file/manage/returnFile',
                    data: JSON.stringify(data.field),
                    dataType: 'json',
                    contentType: "application/json;charset=utf-8",
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.message, {icon: 1, time: 3000}, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.layui.table.reload("valid");
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 3000});
                        }
                    }
                })
            }
            return false;
        })
    })
</script>
</body>
</html>